﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns:fn="http://www.w3.org/2005/xpath-functions"
                xmlns:rdm="urn:rdm-data"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                exclude-result-prefixes="xs fn rdm msxsl">
        <xsl:output method="xml"
                    version="1.0"
                    encoding="utf-8"
                    indent="yes"
                />
    <!--<msxsl:script implements-prefix="rdm" language="CSharp">
        
        
    </msxsl:script>-->

    <xsl:param name="ServiceName" select="'Service'" />
    <xsl:param name="SolutionFolder" select="'..\..\AcceleratorService'" />
        <!--<xsl:choose>
            <xsl:when test="(function-available('fn:environment-variable')) and boolean(fn:environment-variable('_acceleratorSolution'))">
                <xsl:value-of select="string(fn:environment-variable('_acceleratorSolution'))"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="string('..\\..\\..\\..\\AcceleratorService')"/>
            </xsl:otherwise>
        </xsl:choose>--> 

    <!--<xsl:param name="accfgFile" select="concat($SolutionFolder, '\\', $ServiceName, 'Accelerator.accfg')" />
    <xsl:param name="cscfgFile" select="concat($SolutionFolder, '\\', $ServiceName, 'Configuration.cscfg')" />
    <xsl:param name="csdefFile" select="concat($SolutionFolder, '\\', $ServiceName, 'Definition.csdef')" />-->
    <!--<xsl:param name="cscfg" select="document($cscfgFile)" />
    <xsl:param name="csdef" select="document($csdefFile)" />
    <xsl:param name="accfg" select="document($accfgFile)" />-->


    <xsl:param name="cfgFile" select="'C:\_Source\_CodePlex\ac\trunk\AcceleratorService\ServiceDefinition.csdef'" />
    <xsl:param name="cfg" select="document($cfgFile)" />

    <!--<xsl:include href="ServiceMerge.xslt" />-->
    <xsl:strip-space elements="*" />
    
    <xsl:template match="/*">
        
        <!--[ Service Accelerator ]-->
        <xsl:copy>

            <!--<xsl:call-template name="Attributes">
                <xsl:with-param name="a" select="." />
                <xsl:with-param name="b" select="$cfg/." />
            </xsl:call-template>-->

            <xsl:for-each select="*">
                <xsl:copy>
                    <xsl:choose>
                        <xsl:when test="@name = $cfg/*/*/@name">
                            <xsl:call-template name="merge">
                                <xsl:with-param name="std" select="."/>
                                <xsl:with-param name="ovrd" select="$cfg/*/*/."/>
                            </xsl:call-template>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:copy-of select="." />
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:copy>
            </xsl:for-each>
            
            <!--<xsl:call-template name="merge">
                <xsl:with-param name="std" select="."/>
                <xsl:with-param name="ovrd" select="$cfg/*/."/>
            </xsl:call-template>
                        
            <xsl:choose>
                <xsl:when test="@name = $cfg/*/@name">

                    <xsl:call-template name="merge">
                        <xsl:with-param name="std" select="."/>
                        <xsl:with-param name="ovrd" select="$cfg/*[@name = @name]"/>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:copy-of select="." />
                </xsl:otherwise>
            </xsl:choose>-->
            <!--<xsl:for-each select="@name = $csdef/*/@name">
                <xsl:copy>
                    <xsl:for-each select="*">
                        <xsl:copy>
                            <xsl:copy-of select="*"/>
                            <xsl:copy-of select="$csdef/*/*[local-name() = local-name(.)]/*" />
                        </xsl:copy>
                    </xsl:for-each>
                    
                </xsl:copy>
            </xsl:for-each>-->
            
            <!--[ Add all service level elements which we don't match. ]-->
            <!--<xsl:for-each select="$csdef/*/*">
                <xsl:if test="not($accfg[local-name() = local-name(.)])" >
                        <xsl:copy-of select="." />
                </xsl:if>
            </xsl:for-each>-->
            
        </xsl:copy>
    </xsl:template>

    
    <xsl:template match="WebRole">
        <xsl:param name="cfgWebRole" />


        
        
    </xsl:template>
    <xsl:template name="Nodes">
        <xsl:param name="ace" />
        <xsl:param name="def" />
        <xsl:param name="cfg" />
        <xsl:for-each select="$ace/*[local-name() != local-name($def/*)]/*">
            <!--<xsl:element namespace="" name="{}">
                <xsl:value-of select="@*"/>
            </xsl:element>-->
        </xsl:for-each>
        
        <xsl:for-each select="$cfg[@* != $def/@*]">
            <xsl:attribute name="{.}">
                <xsl:value-of select="@*"/>
            </xsl:attribute>
        </xsl:for-each>
        <xsl:for-each select="$def/@*">
            <xsl:attribute name="{.}">
                <xsl:value-of select="@*"/>
            </xsl:attribute>
        </xsl:for-each>  
    </xsl:template>

    <xsl:template name="Attributes">
        <xsl:param name="a" />
        <xsl:param name="b" />
        <xsl:for-each select="$a/*/@*[@* != local-name($b/@*)]">
               <xsl:attribute name="{.}">
                    <xsl:value-of select="@*"/>
                </xsl:attribute>
        </xsl:for-each>
        <xsl:for-each select="$b/@*">
            <xsl:attribute name="{.}">
                <xsl:value-of select="@*"/>
            </xsl:attribute>
        </xsl:for-each>
    </xsl:template>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>



    <xsl:template name="merge">
        <xsl:param name="std"/>
        <xsl:param name="ovrd"/>
        <xsl:for-each select="$std/*">
            <xsl:variable name="key" select="@*[1]"/>
            <xsl:variable name="ovr"
               select="$ovrd/*[local-name() = local-name(current()) 
                 and (not($key) or @*[1] = $key)]"/>
            <xsl:if test="count($ovr) = 0">
                <xsl:copy-of select="."/>
            </xsl:if>
            <xsl:if test="count($ovr) = 1 and (not($ovr/@overrideMode) 
                    or $ovr/@overrideMode != 'delete')">
                <xsl:choose>
                    <xsl:when test="count($ovr/*) = 0 or $ovr/@overrideMode = 'update' 
                          or $ovr/@overrideMode = 'replace'">
                        <xsl:variable name="current" select="."/>
                        <xsl:for-each select="$ovr">
                            <xsl:copy>
                                <xsl:for-each select="@*[name() != 'overrideMode']|
                                      text()[string-length(normalize-space(.))>0]">
                                    <xsl:copy/>
                                </xsl:for-each>
                                <xsl:choose>
                                    <xsl:when test="$ovr/@overrideMode = 'replace'">
                                        <xsl:copy-of select="*"/>
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <xsl:call-template name="merge">
                                            <xsl:with-param name="std" select="$current"/>
                                            <xsl:with-param name="ovrd" select="."/>
                                        </xsl:call-template>
                                    </xsl:otherwise>
                                </xsl:choose>
                            </xsl:copy>
                        </xsl:for-each>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:copy>
                            <xsl:for-each select="@*|text()[string-length(normalize-space(.))>0]">
                                <xsl:copy/>
                            </xsl:for-each>
                            <xsl:call-template name="merge">
                                <xsl:with-param name="std" select="."/>
                                <xsl:with-param name="ovrd" select="$ovr"/>
                            </xsl:call-template>
                        </xsl:copy>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:if>
        </xsl:for-each>
        <xsl:for-each select="$ovrd/*">
            <xsl:variable name="key" select="@*[1]"/>
            <xsl:if test="count($std/*[local-name() = local-name(current()) 
                    and (not($key) or @*[1] = $key)]) = 0">
                <xsl:copy-of select="."/>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>
